package com.chhuang.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chhuang.core.mapper.InterfaceMapper;
import com.chhuang.core.service.InterfaceService;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @Description 自定义业务现实类
 * @Author Darren Huang
 * @Date 2023-10-13 13:05
 */
public class ServiceImplementation<M extends InterfaceMapper<T>, T> extends ServiceImpl<M, T> implements InterfaceService<T> {

    @Override
    public boolean exists(Wrapper<T> queryWrapper) {
        return baseMapper.exists(queryWrapper);
    }

    @Override
    public boolean saveBatch(Collection<T> entityList) {
        boolean bool = false;
        if(entityList!=null && !entityList.isEmpty()){
            int len = 1000;
            int size = entityList.size();
            int num = size/len;
            for(int i=0; i<num; i++){
                Collection<T> temps = entityList.stream()
                        //使用skip(10)方法跳过前10个元素，使用limit(990)方法获取接下来的990个元素
                        .skip(i*len).limit(len)
                        .collect(Collectors.toList());
                baseMapper.insertBatchSomeColumn(temps);
            }
            int remainder = size % len;
            if(remainder > 0) {
                baseMapper.insertBatchSomeColumn(entityList.stream()
                        .skip(num*len).limit(remainder)
                        .collect(Collectors.toList()));
            }
            bool = true;
        }
        return bool;
    }
}
